/**   
* @Title: PictureServiceImpl.java 
* @Package com.taotao.service.impl 
* @Description: TODO(用一句话描述该文件做什么) 
* @author hekai   
* @date 2017年1月8日 下午10:06:09 
* @version V1.0   
*/
package com.taotao.service.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.taotao.common.pojo.PictureResult;
import com.taotao.common.utils.FastDFSClient;
import com.taotao.service.PictureService;

/** 
* @ClassName: PictureServiceImpl 
* @Description: 图片上传service 
* @author hekai
* @date 2017年1月8日 下午10:06:09 
*  
*/
@Service
public class PictureServiceImpl implements PictureService {
	
	private Logger logger = LoggerFactory.getLogger(PictureServiceImpl.class);
	
	@Value("${IMG_SERVER_BASE_URL}")
	private String IMG_SERVER_BASE_URL;

	/* (non-Javadoc)
	 * @see com.taotao.service.PictureService#uploadPic(org.springframework.web.multipart.MultipartFile)
	 */
	@Override
	public PictureResult uploadPic(MultipartFile picFile) {
		PictureResult result = new PictureResult();
		//判断图片是否为空
		if(picFile.isEmpty()){
			result.setMessage("图片为空");
			result.setError(1);
			logger.info("图片为空");
			return result;
		}
		
		//上传到图片服务器
		try {
			//取图片的扩展名
			String originalFilename = picFile.getOriginalFilename();
			String extName = originalFilename.substring(originalFilename.indexOf(".") + 1);
			FastDFSClient client = new FastDFSClient("classpath:properties/client.conf");
			String url = client.uploadFile(picFile.getBytes(), extName);
			result.setError(0);
			result.setUrl(IMG_SERVER_BASE_URL + url);
		} catch (Exception e) {
			logger.info("上传图片失败，错误信息：{}", e);
			result.setMessage("图片上传失败！");
			result.setError(1);
		}
		return result;
	}

}




















